package com.leiqichao.backend.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.Data;

import java.util.List;

/**
 * 分页工具类
 *
 * @param <T> 分页数据的类型
 */
public class Page<T> implements IPage<T> {

    private int pageNum;     // 当前页码
    private int pageSize;    // 每页大小
    private int total;       // 总记录数
    private int totalPages;  // 总页数
    private List<T> records;    // 当前页的数据列表
    private T queryParam;

    /**
     * 构造函数
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     */
    public Page(int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    /**
     * 计算分页的偏移量（用于数据库查询）
     *
     * @return 偏移量
     */
    public int getOffset() {
        return (pageNum - 1) * pageSize;
    }


    @Override
    public List<OrderItem> orders() {
        return List.of();
    }

    @Override
    public List<T> getRecords() {
        return records;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return total;
    }

    @Override
    public IPage<T> setTotal(long total) {
        this.total = (int) total;
        this.totalPages = (int) Math.ceil((double) total / pageSize);
        return this;
    }

    @Override
    public long getSize() {
        return pageSize;
    }

    @Override
    public IPage<T> setSize(long size) {
        this.pageSize = (int) size;
        return this;
    }

    @Override
    public long getCurrent() {
        return pageNum;
    }

    @Override
    public IPage<T> setCurrent(long current) {
        this.pageNum = (int) current;
        return this;
    }

    public T getQueryParam() {
        return queryParam;
    }

    public void setQueryParam(T queryParam) {
        this.queryParam = queryParam;
    }

    public int getPageNum() {
        return totalPages;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", total=" + total +
                ", totalPages=" + totalPages +
                ", records=" + records +
                '}';
    }
}